package com.nulldev.util.web.flexibleKit.flexRenderer.paths.subgens;

import com.nulldev.util.graphics.renderIt.api.renderItGraphicsInterface;
import com.nulldev.util.graphics.renderIt.api.core.RITFont;
import com.nulldev.util.graphics.renderIt.renderer.global.rColor;
import com.nulldev.util.graphics.renderIt.util.renderPosition;
import com.nulldev.util.web.flexibleKit.flexRenderer.FlexEnvironment;
import com.nulldev.util.web.flexibleKit.flexRenderer.paths.PathGenerator;
import com.nulldev.util.web.flexibleKit.flexRenderer.styling.fonts.flexFontManager;
import com.nulldev.util.web.flexibleKit.general.RenderOperation;
import com.nulldev.util.web.flexibleKit.global.GlobalRendererSettings;
import com.nulldev.util.web.flexibleKit.jfvParse.Node;

import static com.nulldev.util.web.flexibleKit.flexRenderer.paths.subgens.ParagraphGenerator.isBlankLine;

import com.nulldev.util.VariableAPI.ArrayUtils;

public class StrongGenerator implements PathGenerator {

	@Override
	public RenderOperation generate(final Node n, final PathGenerationFlags... flags) {
		final RenderOperation ri = new RenderOperation() {

			private RITFont font;

			@Override
			public renderPosition doRender(final renderItGraphicsInterface i, final renderPosition rp, final FlexEnvironment imgCache) {
//				final renderPosition legacy = (renderPosition) i.getExtensionValue(RequestableExtensions.FR_EXT_PREV_POSITION);
//				rp.set(legacy);
				final int fontSize = GlobalRendererSettings.SCALE_TEXT_AUTOMATICALLY ? i.viewport_getWidth() / 32 : flexFontManager.getFontSize("strong");
				final String s = n.getTextContent();
				if (isBlankLine(s))
					return rp;
				if (font == null)
					font = i.awt_make_font(flexFontManager.getFont("sans-serif").getFont(fontSize).deriveFont(java.awt.Font.BOLD));
				// FIXME: Use text_scalable
				i.text_string(s, rp.getX() + imgCache.defaultOffset, rp.getY(), font, rColor.BLACK);
//				i.requestViewportHeight(rp.getY());
				return rp.clone().addX(fontSize * s.length()).addX(imgCache.defaultOffset);
			}

			@Override
			public Node operationFor() {
				return n;
			}

			@Override
			public boolean keepInline() {
				return ArrayUtils.contains(flags, PathGenerationFlags.RENDER_INLINE);
			}
		};
		return ri;
	}
}
